package com.xiaoshuaila.workbench.controller;

import com.xiaoshuaila.settings.domain.user;
import com.xiaoshuaila.tool.DateTimeUtil;
import com.xiaoshuaila.tool.PrintJson;
import com.xiaoshuaila.tool.ServiceFactory;
import com.xiaoshuaila.tool.UUIDUtil;
import com.xiaoshuaila.workbench.domain.Activity;
import com.xiaoshuaila.workbench.domain.Activityremark;
import com.xiaoshuaila.workbench.service.Activityservice;
import com.xiaoshuaila.workbench.service.ActivityserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

public class Activitycontroller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入市场活动控制器");
        String path=req.getServletPath();
        Activityservice activityservice=
                (Activityservice) ServiceFactory.getService(new ActivityserviceImpl());
        if ("/workbench/activity/xxx.do".equals(path))
        {

            System.out.println("xxx.do");
            List<user>list=activityservice.findalluser();
            PrintJson.printJsonObj(resp,
                    list);
        }
        else if("/workbench/activity/save.do".equals(path))
        {
            System.out.println("save.do");
            Activity activity=new Activity();
            activity.setId(UUIDUtil.getUUID());
            activity.setOwner(req.getParameter("owner"));
            activity.setName(req.getParameter("name"));
            activity.setStartDate(req.getParameter("startDate"));
            activity.setEndDate(req.getParameter("endDate"));
            activity.setCreateTime(DateTimeUtil.getSysTime());
            activity.setCreateBy(req.getParameter("createBy"));

            System.out.println(req.getParameter("endDate"));
            activity.setCost(req.getParameter("cost"));
            activity.setDescription(req.getParameter("description"));
            int i=activityservice.save(activity);
            if (i==1)PrintJson.printJsonFlag(resp,true);
            else PrintJson.printJsonFlag(resp,false);
        }
        else if("/workbench/activity/pagelist.do".equals(path))
        {

            System.out.println("pagelist.do");
//            List<Activityvo>list=
//                    activityservice.pagelist(req.getParameter("name"),
//                            req.getParameter("李文丰"),
//                            req.getParameter("startDate"),
//                            req.getParameter("endDate"),
//                            (Integer.valueOf(req.getParameter(
//                                    "pageNo"))-1)*Integer.valueOf(req.getParameter(
//                                    "pageSize")),
//                            Integer.valueOf(req.getParameter(
//                                    "pageSize")));
            Map serch=new HashMap();
            serch.put("name",req.getParameter("name"));
            serch.put("owner",req.getParameter("owner"));
            serch.put("startDate",req.getParameter("startDate"));
            serch.put("endDate",req.getParameter("endDate"));
            serch.put("pageNo",(Integer.valueOf(req.getParameter(
                    "pageNo"))-1)*Integer.valueOf(req.getParameter(
                    "pageSize")));
            serch.put("pageSize",Integer.valueOf(req.getParameter(
                    "pageSize")));
            PrintJson.printJsonObj(resp,activityservice.pagelist(serch));
        }
        else if("/workbench/activity/delete.do".equals(path))
        {
            String id[]=req.getParameterValues("id");

            PrintJson.printJsonFlag(resp,activityservice.delect(id));
        }
        else if ("/workbench/activity/updatefind.do".equals(path))
        {
            System.out.println("updatefind");
            PrintJson.printJsonObj(resp,
                    activityservice.update(req.getParameter("id")));
        }
        else if ("/workbench/activity/update.do".equals(path))
        {
            System.out.println("update");
            Activity activity=new Activity();
            activity.setId(req.getParameter("id"));
            activity.setName(req.getParameter("name"));
            activity.setOwner(req.getParameter("owner"));
            activity.setStartDate(req.getParameter("startDate"));
            activity.setEndDate(req.getParameter("endDate"));
            activity.setCost(req.getParameter("cost"));
            activity.setDescription(req.getParameter("description"));
            activity.setEditBy(req.getParameter("editBy"));
            activity.setEditTime(DateTimeUtil.getSysTime());

            if (activityservice.update1(activity)==0)
                PrintJson.printJsonFlag(resp,false);
            else PrintJson.printJsonFlag(resp,true);
        }
        else if ("/workbench/activity/detail.do".equals(path))
        {
            System.out.println("datail");
            Activity activity=activityservice.
                    datail(req.getParameter("id"));
            req.setAttribute("activity",activity);
            req.getRequestDispatcher("/workbench/activity/detail.jsp")
                    .forward(req,resp);
        }
        else if ("/workbench/activity/getremark.do".equals(path))
        {
            System.out.println("getremark");
            PrintJson.printJsonObj(resp,
                    activityservice.selectrmark(req.getParameter(
                            "id")));
        }
        else if ("/workbench/activity/deleteremark.do".equals(path))
        {
            System.out.println("deleteremark");
            if (activityservice.deleteremark(req.getParameter("id"))==1)
            {
                PrintJson.printJsonFlag(resp,true);
            }
            else PrintJson.printJsonFlag(resp,false);
        }
        else if ("/workbench/activity/saveremark.do".equals(path))
        {
            System.out.println("saveremark");
            Activityremark activityremark=new Activityremark();
            activityremark.setCreateBy(req.getParameter("createBy"));
            activityremark.setCreateTime(DateTimeUtil.getSysTime());
            activityremark.setNoteContent(req.getParameter("noteContent"));
            activityremark.setActivityId(req.getParameter("activityId"));
            activityremark.setId(UUIDUtil.getUUID());
            if (activityservice.insertremark(activityremark)==1)
                PrintJson.printJsonFlag(resp,true);
            else PrintJson.printJsonFlag(resp,false);
            System.out.println(req.getParameter("createBy"));
        }
        else if ("/workbench/activity/updateremark.do".equals(path))
        {
            System.out.println("updateremark");
            Activityremark activityremark=new Activityremark();
            activityremark.setId(req.getParameter("id"));
            activityremark.setEditBy(req.getParameter("editBy"));
            activityremark.setEditTime(DateTimeUtil.getSysTime());
            activityremark.setNoteContent(req.getParameter("noteContent"));
            if (activityservice.updateremark(activityremark)==1)
            PrintJson.printJsonFlag(resp,true);
            else PrintJson.printJsonFlag(resp,false);
        }
    }
}
